package com.gangquan360.smartadmin.module.samplesend.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.login.domain.RequestTokenBO;
import com.gangquan360.smartadmin.module.samplesend.dao.SampleSendDao;
import com.gangquan360.smartadmin.module.samplesend.dao.SampleSendProductDao;
import com.gangquan360.smartadmin.module.samplesend.dao.SampleSendTrackLogDao;
import com.gangquan360.smartadmin.module.samplesend.domain.dto.SampleSendDTO;
import com.gangquan360.smartadmin.module.samplesend.domain.dto.SampleSendProductDTO;
import com.gangquan360.smartadmin.module.samplesend.domain.dto.SampleSendQueryDTO;
import com.gangquan360.smartadmin.module.samplesend.domain.dto.SampleSendTrackDTO;
import com.gangquan360.smartadmin.module.samplesend.domain.dto.SampleSendTrackLogDTO;
import com.gangquan360.smartadmin.module.samplesend.domain.entity.SampleSendEntity;
import com.gangquan360.smartadmin.module.samplesend.domain.entity.SampleSendProductEntity;
import com.gangquan360.smartadmin.module.samplesend.domain.entity.SampleSendTrackLogEntity;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartRequestTokenUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * [ 产品送样表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-28 17:47:14
 * @since JDK1.8
 */
@Service
@Slf4j
public class SampleSendService {

	@Autowired
	private SampleSendDao sampleSendDao;

	@Autowired
	private SampleSendProductDao sampleSendProductDao;
	
	@Autowired
	private SampleSendTrackLogDao sampleSendTackLogDao;

	/**
	 * @author wiggin
	 * @description 分页查询
	 * @date 2020-03-28 17:47:14
	 */
	public ResponseDTO<PageResultDTO<SampleSendDTO>> queryByPage(SampleSendQueryDTO queryDTO) {
		Page<SampleSendDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
		List<SampleSendEntity> entities = sampleSendDao.queryByPage(page, queryDTO);
		List<SampleSendDTO> dtoList = SmartBeanUtil.copyList(entities, SampleSendDTO.class);
		if (CollectionUtils.isNotEmpty(entities)) {
			List<Long> sendIds = entities.parallelStream().map(SampleSendEntity::getId).collect(Collectors.toList());
			List<SampleSendProductDTO> selectListBySendIds = sampleSendProductDao
					.selectListBySendIds(sendIds);
			 Map<Long, List<SampleSendProductDTO>> sampleSendPrdouctMap = selectListBySendIds.parallelStream()
					.collect(Collectors.groupingBy(SampleSendProductDTO::getSampleSendId));
			dtoList.forEach(item -> {
				List<SampleSendProductDTO> list = sampleSendPrdouctMap.get(item.getId());
				if (CollectionUtils.isNotEmpty(list)) {
					item.setProductList(list);
				}
			});
		}
		page.setRecords(dtoList);
		PageResultDTO<SampleSendDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
		return ResponseDTO.succData(pageResultDTO);
	}

	/**
	 * @author wiggin
	 * @description 添加
	 * @date 2020-03-28 17:47:14
	 */
	@Transactional(rollbackFor = Exception.class)
	public ResponseDTO<String> add(SampleSendDTO addDTO) {
		SampleSendEntity entity = SmartBeanUtil.copy(addDTO, SampleSendEntity.class);
		sampleSendDao.insert(entity);
		List<SampleSendProductDTO> productList = addDTO.getProductList();
		if (CollectionUtils.isNotEmpty(productList)) {
			List<SampleSendProductEntity> productEntityList = SmartBeanUtil.copyList(productList,
					SampleSendProductEntity.class);
			productEntityList.parallelStream().forEach(item -> {
				item.setSampleSendId(entity.getId());
				sampleSendProductDao.insert(item);
			});
		}
		return ResponseDTO.succ();
	}

	/**
	 * @author wiggin
	 * @description 编辑
	 * @date 2020-03-28 17:47:14
	 */
	@Transactional(rollbackFor = Exception.class)
	public ResponseDTO<String> update(SampleSendDTO updateDTO) {
		SampleSendEntity entity = SmartBeanUtil.copy(updateDTO, SampleSendEntity.class);
		sampleSendDao.updateById(entity);
		return ResponseDTO.succ();
	}

	/**
	 * @author wiggin
	 * @description 删除
	 * @date 2020-03-28 17:47:14
	 */
	@Transactional(rollbackFor = Exception.class)
	public ResponseDTO<String> delete(Long id) {
		sampleSendDao.deleteById(id);
		return ResponseDTO.succ();
	}

	/**
	 * @author wiggin
	 * @description 根据ID查询
	 * @date 2020-03-28 17:47:14
	 */
	public ResponseDTO<SampleSendDTO> detail(Long id) {
		SampleSendEntity entity = sampleSendDao.selectById(id);
		SampleSendDTO dto = SmartBeanUtil.copy(entity, SampleSendDTO.class);
		return ResponseDTO.succData(dto);
	}
	
	/**
	 * 
	 * @param sampleSendTrackDTO
	 * @return
	 */
	public ResponseDTO<String> addTrack(@RequestBody @Valid SampleSendTrackDTO sampleSendTrackDTO) {
		SampleSendTrackLogEntity entity = SmartBeanUtil.copy(sampleSendTrackDTO, SampleSendTrackLogEntity.class);
		 RequestTokenBO requestToken = SmartRequestTokenUtil.getThreadLocalUser();
		entity.setCreateUser(requestToken.getRequestUserId());
		sampleSendTackLogDao.insert(entity);
		return ResponseDTO.succ();
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public ResponseDTO<List<SampleSendTrackLogDTO>> getTrackList(Long id) {
		 List<SampleSendTrackLogDTO> selectListBySendId = sampleSendTackLogDao.selectListBySendId(id);
		 return ResponseDTO.succData(selectListBySendId);
	}
}
